import logging

from base.Base import AirtestUtils
from config.config import TEST_CONFIG

class LoginPage:


    def Login_Test(self):

        # 参数化配置文件
        templates = TEST_CONFIG["templates"]
        bases = AirtestUtils
        # 初始化设备与页面
        AirtestUtils.connect_device(TEST_CONFIG["device_uri"])
        AirtestUtils.start_app(TEST_CONFIG["package_name"])
        # bases.click_element(templates["gw"],action_name="购物")
        # 进入“我的”
        bases.click_element(TEST_CONFIG["my_tab"],action_name="我的标签页")
        # 输入账号
        bases.clear_and_input_text(templates["phone_input"], TEST_CONFIG["test_account"], action_name="输入手机号")
        # 输入密码
        bases.input_text(templates["password_input"],action_name="密码输入框")
        # 勾选协议
        bases.click_element(templates["agree_checkbox"],action_name="用户协议勾选框")
        # 点击登录
        bases.click_element(templates["login_button"],action_name="登录按钮")
        """验证登录成功"""
        # 验证结果
        assert bases.assert_png(templates["success_flag"], action_name="用户头像")
        logging.info("✅ 登录验证成功！")



    # """登录页面 PO 类（封装页面元素与操作）"""
    # def __init__(self):
    #     self.templates = TEST_CONFIG["templates"]
    #     self.utils = AirtestUtils
    #
    # def go_to_my_tab(self):
    #     """进入“我的”标签页"""
    #     self.utils.click_element(
    #         self.templates["my_tab"],
    #         action_name="我的标签页"
    #     )
    #
    # def input_phone(self, phone):
    #     """输入手机号"""
    #     self.utils.input_text(
    #         self.templates["phone_input"],
    #         text=phone,
    #         action_name="手机号输入框"
    #     )
    #
    # def input_password(self, password):
    #     """输入密码"""
    #     self.utils.input_text(
    #         self.templates["password_input"],
    #         text=password,
    #         action_name="密码输入框"
    #     )
    #
    # def check_agree(self):
    #     """勾选协议"""
    #     self.utils.click_element(
    #         self.templates["agree_checkbox"],
    #         action_name="用户协议勾选框",
    #         threshold=0.6  # 按需调整阈值
    #     )
    #
    # def click_login(self):
    #     """点击登录按钮"""
    #     self.utils.click_element(
    #         self.templates["login_button"],
    #         action_name="登录按钮"
    #     )
    #
    # def verify_login_success(self):
    #     """验证登录成功"""
    #     try:
    #         self.utils.click_element(
    #             self.templates["success_flag"],
    #             action_name="登录成功标识",
    #             timeout=10
    #         )
    #         print("✅ 登录验证通过！")
    #         return True
    #     except Exception as e:
    #         raise Exception(f"❌ 登录验证失败: {str(e)}")